package com.shwread.android.ui.dialog;


import android.app.ActionBar.LayoutParams;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shwread.android.qysw10000204.R;


public class OrderSeatTipDialog
        extends
        Dialog
{
	private Context context;
	private int value;
	private String location, perNum, time;
	private DatePickerListener listener;
	private LinearLayout llWait, llSuccess;
	private TextView tvBoxError, tvPerNumError, tvTimeError, tvLocation, tvPersonNumber, tvTime;
	private Button ok;
	private NotificationManager notifiManager;
	private Notification        notif;
	
	public OrderSeatTipDialog(Context context, int value, DatePickerListener listener) {
		super(context, R.style.common_dialog);
		this.context = context;
		this.value = value;
		this.listener = listener;
	}
	public OrderSeatTipDialog(Context context, int value, String location, DatePickerListener listener) {
		super(context, R.style.common_dialog);
		this.context = context;
		this.value = value;
		this.location = location;
		this.listener = listener;
	}
	public OrderSeatTipDialog(Context context, int value, String location, String perNum, String time, DatePickerListener listener) {
		super(context, R.style.common_dialog);
		this.context = context;
		this.value = value;
		this.location = location;
		this.perNum = perNum;
		this.time = time;
		this.listener = listener;
	}
    @Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setCanceledOnTouchOutside(true);
	    initView();
	    getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	    getWindow().setGravity(Gravity.CENTER);
	    getWindow().setWindowAnimations(android.R.style.Animation_InputMethod);
	}

	private void initView() {
		View view = LayoutInflater.from(context).inflate(R.layout.order_seat_tip, null);
		llWait = (LinearLayout) view.findViewById(R.id.ll_order_seat_tip_wait);
		llSuccess = (LinearLayout) view.findViewById(R.id.ll_order_seat_tip_success);
		tvBoxError = (TextView) view.findViewById(R.id.tv_order_seat_tip_box_error);
		tvPerNumError = (TextView) view.findViewById(R.id.tv_order_seat_tip_person_number_error);
		tvTimeError = (TextView) view.findViewById(R.id.tv_order_seat_tip_time_error);
		tvLocation = (TextView) view.findViewById(R.id.tv_order_seat_tip_location);
		tvPersonNumber = (TextView) view.findViewById(R.id.tv_order_seat_tip_person_number);
		tvTime = (TextView) view.findViewById(R.id.tv_order_seat_tip_time);
		ok = (Button) view.findViewById(R.id.btn_order_seat_tip_sure);
		switch(value) {
		case 0:
			setCanceledOnTouchOutside(false);
			llWait.setVisibility(View.VISIBLE);
			llSuccess.setVisibility(View.GONE);
			tvBoxError.setVisibility(View.GONE);
			tvPerNumError.setVisibility(View.GONE);
			tvTimeError.setVisibility(View.GONE);
//			if (notifiManager == null)
//			{
//				notifiManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
//			}
//			notifiManager.cancel(1010);
//			if (notif != null)
//			{
//				notif = null;
//			}
//			Intent intent = new Intent();
//			intent.setClass(context, MyAppointmentListActivity.class);
//		    PendingIntent pendingIntent= PendingIntent.getActivity(context, 0, intent, 0);
//			NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
//			mBuilder.setContentTitle("成功预约"+location)//设置通知栏标题
//			.setContentText("请提前15分钟到场，超过预约时间将自动失效！") //设置通知栏显示内容
//			.setContentIntent(pendingIntent) //设置通知栏点击意图
//			.setTicker("测试通知来啦") //通知首次出现在通知栏，带上升动画效果的
//			.setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
//			.setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
//			.setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消  
//			.setDefaults(Notification.DEFAULT_ALL)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合
//			//Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
//			.setSmallIcon(R.drawable.ic_launcher);//设置通知小ICON
//			notif = mBuilder.build();
//			notif.flags = Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
//			notifiManager.notify(1010, notif);
			break;
		case 1:
			llWait.setVisibility(View.GONE);
			llSuccess.setVisibility(View.VISIBLE);
			tvBoxError.setVisibility(View.GONE);
			tvPerNumError.setVisibility(View.GONE);
			tvTimeError.setVisibility(View.GONE);
			tvLocation.setText(location);
			tvPersonNumber.setText(perNum+"人");
			tvTime.setText(time);
			break;
		case 2:
			llWait.setVisibility(View.GONE);
			llSuccess.setVisibility(View.GONE);
			tvBoxError.setVisibility(View.VISIBLE);
			tvPerNumError.setVisibility(View.GONE);
			tvTimeError.setVisibility(View.GONE);
			break;
		case 3:
			llWait.setVisibility(View.GONE);
			llSuccess.setVisibility(View.GONE);
			tvBoxError.setVisibility(View.GONE);
			tvPerNumError.setVisibility(View.GONE);
			tvTimeError.setVisibility(View.VISIBLE);
			break;
		case 4:
			llWait.setVisibility(View.GONE);
			llSuccess.setVisibility(View.GONE);
			tvBoxError.setVisibility(View.GONE);
			tvPerNumError.setVisibility(View.VISIBLE);
			tvTimeError.setVisibility(View.GONE);
			break;
		}
		setContentView(view);
		ClickListener l = new ClickListener();
		ok.setOnClickListener(l);
    }
	
	private class ClickListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_order_seat_tip_sure:
				if(value==0) {
					listener.onSelect("wait");
				} else if(value==1) {
					listener.onSelect("success");
				}
				dismiss();
				break;
			}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			if(value==0 || value==1) {
				setCanceledOnTouchOutside(false);
				return false;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	
}
